/**
 * 
 */
package furniture.items3d.model.tessellation;

import javax.vecmath.Point3d;

import org.eclipse.swt.graphics.RGB;

import furniture.items3d.model.CuboidPrimitive;
import furniture.render3d.Line3d;
import furniture.render3d.PointSet;
import furniture.render3d.TriangleSet;

/**
 * @author balage
 *
 */
public class CuboidTessellator {

	public TriangleSet tessellate(CuboidPrimitive cuboid){
		Triangles result = new Triangles();
		
		double x1 = cuboid.startpoint.x;
		double y1 = cuboid.startpoint.y;
		double z1 = cuboid.startpoint.z;
		double x2 = cuboid.endpoint.x;
		double y2 = cuboid.endpoint.y;
		double z2 = cuboid.endpoint.z;
		
		//double l = 0.1;
		
		Point3d p111 = new Point3d(x1, y1, z1);
		Point3d p112 = new Point3d(x1, y1, z2);
		Point3d p121 = new Point3d(x1, y2, z1);
		Point3d p122 = new Point3d(x1, y2, z2);
		Point3d p211 = new Point3d(x2, y1, z1);
		Point3d p212 = new Point3d(x2, y1, z2);
		Point3d p221 = new Point3d(x2, y2, z1);
		Point3d p222 = new Point3d(x2, y2, z2);
		
//		x1 -= l;
//		y1 -= l;
//		z1 -= l;
//		x2 += l;
//		y2 += l;
//		z2 += l;
//		
//		Point3d l111 = new Point3d(x1, y1, z1);
//		Point3d l112 = new Point3d(x1, y1, z2);
//		Point3d l121 = new Point3d(x1, y2, z1);
//		Point3d l122 = new Point3d(x1, y2, z2);
//		Point3d l211 = new Point3d(x2, y1, z1);
//		Point3d l212 = new Point3d(x2, y1, z2);
//		Point3d l221 = new Point3d(x2, y2, z1);
//		Point3d l222 = new Point3d(x2, y2, z2);
		
		
		result.addTriangle(ps(p111, p211, p221, p121));
		result.addTriangle(ps(p112, p212, p222, p122));
		
		result.addTriangle(ps(p111, p112, p122, p121));
		result.addTriangle(ps(p211, p212, p222, p221));
		
		result.addTriangle(ps(p111, p211, p212, p112));
		result.addTriangle(ps(p121, p221, p222, p122));
		
//		result.addTriangle(ln(l111, l112));
//		result.addTriangle(ln(l121, l122));
//		result.addTriangle(ln(l211, l212));
//		result.addTriangle(ln(l221, l222));
//		
//		result.addTriangle(ln(l111, l121));
//		result.addTriangle(ln(l112, l122));
//		result.addTriangle(ln(l211, l221));
//		result.addTriangle(ln(l212, l222));
//		
//		result.addTriangle(ln(l111, l211));
//		result.addTriangle(ln(l112, l212));
//		result.addTriangle(ln(l121, l221));
//		result.addTriangle(ln(l122, l222));
		
		return result;
	}
	
	public PointSet ps(Point3d p1, Point3d p2, Point3d p3, Point3d p4){
		PointSet r = new PointSet(new Point3d(p1), new Point3d(p2), new Point3d(p3), new Point3d(p4));
		r.paint = new RGB(200, 200, 200);
		r.color = new RGB(0, 0, 0);
		r.fill = true;
		return r;
	}
	
	public Line3d ln(Point3d p1, Point3d p2){
		return new Line3d(new Point3d(p1), new Point3d(p2), new RGB(0,0,0));
	}
	
}
